Working prescription, handles error and properly modularized. Added closeConnection call in main on exit