Use URI.encode_query instead of self-made