Refactering duplicate code