Better sort cache, fixes #603