fixed logic for the insertion