fix sorting as mentioned in #33, #113 and #131