Hopefully final fix for crappy SQL