I’ve found that if my code is turning into in impenetrable mass of nested conditionals, then it might be worth seeing if the overall logic can be expressed as a state machine (in general it’s a big topic, but I’m sure there are plenty of easy practical tutorials in case you’re not familiar).