Some re-factorings, and WIP fixing bug of writing to output file
Updating to the database
Can now effectively write to the database