Major refactor and cleanup