Replaced booleans with constants

Refactored the code a little. Fixed an obscure bug with invalid `for` code. Added tests to cover those cases.o
Replaced all character magic numbers with a constant, updated and cleaned up the comments