Refactor to simplify logic