Refactor to make logic clearer