Hopefully final SQL fixes