fixed logic for insert