Split out write_vals into separate function