refactor duplicate codes