refactor duplicated code in controllers