added support for custom ordering, fixed bug in ordering