Initial refactor - untested