Code cleaning
Got rid of a switch statement, tracked down global variables and fixed a few bugs on the way