writes to database