nearly complete refactor