Another round of SQL fixes