Some fixes to make sort pass the type checker