Lots of refactoring and adding an initial UDAF