fixed logic and added test