added commenting and cleaned up code