General cleanup. Also inverted the ordering of some of the if-statements so I could remove the second comparison from the statement.
Lastly, some minor cleanup and fixed a few bugs.