expose sorting option, clean up code, add tests