Refactor to make logic simpler