patched sql code
Merge branch 'cleanup'