some reordering and cleaning up