Started moving subfunctions to top of the function

Not done yet.