refactor, continued. still untested.