added comments, debug, rearranged some code