Rework insert/update and fix header reading