Modified to use the database only when writing.