Major refactor and clean-up