Altered code for writing results to DB