Hi sabbatarian7,
first of all welcome to the forum since this is your first post.
Your point is clear, you are breaking a pretty essential design rule: keep business logic and UI separated.
Of course there are situations where you need code depending on specific actions on the form and/or elements, but this should always be code in private functions.
Besides when you are using a headless client, be aware that it can not use forms (or at least the UI part of it)
Essentially all code executed within the headless client should be from functions in your top level scopes (ie. globals)
So take out the business logic from the code you’re working on and use this in your headless client.
This should be possible, however hard to explain since we don’t know the exact usecase.
Nevertheless: Hope this is pointing you in the right direction.