Major rework of database writing