updated database write function