added call to close db connection when done
debugging output
tried to improve error handling