rearranged code, added inline documentation