Updated code to better handle URLs