major cleanup and refactor