Fixed getValidURL method to cater for corner cases