swapped large chunks of code around