some code cleanup / rearranging