Cleaned up the code, added comments, and reorganized the heirarchy.