Cleaned up and reorganized code.