Back-end sql fixes