Fixed some bugs cases that involved SQL