rearranged some code, fixed comments