Fixed a bug where some elements would not get sorted correctly