Refactor to reduce duplicate code