Rearranged functions, tidied up some stuff.