refactor out a whole big chunk of code