copied lookup code to insert, should be kind of similar