wrote insert logic