refactored and removed duplicate code