refactored logic
fixed logic