Fixed SQL bug causing a fatal error