Better support for faulty URLs, clean up code