Refactor to simplify functions