refactoring complete. Need to put it into a plugin for further testing.
Need to write some documentation