Massive cleanup and refactor