'Functionized' code for reuse and added db check

'Functionized' current code to allow re-use as the script evolves
Added a database check at the start to allow option to process existing database file