added comments, rearranged code to be more readable