Reformatted this file, to have embedded functions, which I believes makes it easier to parse.

Could definitely cause some regression errors...