Massive code cleanup/reordering