code refactor, reduce repeated code