fixed sql closing prematurely should fix issue #7