added comments
added comments, rearranged some code