code cleanup and wip of insert/update