Fixed some SQL bugs only to discover more