large refactor, split out code