massive cleanup and refactor