refactor to reduce repeated code