Lots of cleaning and moving functions to modules.