Fixed SQL bug