Added code to write to database