Code Cleanup

Cleaned up a lot of code. Made use of classes. Also worked to remove all
state transition bugs. (Some still remain)