fixed logic
changed complexity