cleaned up some code, plus entered comments above each function