Check for null urls and improve code, fixes #2589