Reorganized and cleaned up code.