refactor to extract all logic from controller