fixing sql bugs