Major cleanup of code and comments