refactoring some duplicate codes