Sort functions, fixes #1