Extract function to duplicate codes