Some optimisations, fringe-case checks, and deadlock fixes.