Clean up the code a little and minor refactoring to get ready for the UDAF version