fixed a few bugs, found out that sorting is not enough