Fixing sql bug