refactor controller logic into factory