Writes to db