Added commenting and cleaned up code