Fixed sortings. Closes #5, #6, #12