Refactor to simplify some code