Refactoring goal of reducing the number of lines of the code. Removed commented out code, empty white lines. Added a few small functions for commented out code. Moved variable declarations into block where they are used. Collapsed as many if statements as possible.