I refactored some code to be easier to read.
- Some conditions were negated to be able to return/continue early, rather than having the majority of the code inside an if-body.
- Conditions were simplified (!(a == b)) turned into (a != b); repeated conditions calculated only once.
- Unnecessary variables and one unnecessary condition were eliminated