major code cleanup, testing refactoring