added comments

documented the main routine a bit more and rearranged some of the
subroutines