Added tests and fixed issue with sort order