Writes to database correctly