added NP check to avoid NPE which could occur on invalid elements of a range query
added urlencode() function
less logging to console