Massive refactoring update. Cleaned up most of the code.