refactoring duplicate codes