Stats/SQL bug fixes. Fixes issue #5.