in theory this does the whole db write now but needs optimizing