minor commenting and rearranging