major rearrangement of code