major code cleanup for 2.1 and refactoring