Reorganized the code into classes, and cleaned up things here and there