Finished refactoring and resolved TODOs. Also made implementation
agnostic as far as possible.