added comments; rearranged some code