some cleanup and rearrangement