refactor the code to avoid redundancy