fixed some bugs, added sorting