Added write database function