Improve functionality for empty url and refactor code