Major cleanup and rearrangement