refactored the logic