refactoring logic away from controller, push into model