refactoring some controller logic