Redid error handling, ensured db connected before proceeding.

Also cleaned up whitespace and fixed some minor jshint nits.