Add API key error check and better query string detection