some cleanup and rearranging