Add support for RANDOMLY in ordering tuples; fix some bugs