Refactored the code to avoid duplication.