Fix the race condition in ptask_dispose and other cosmetic changes

Rebased the PR sent 2 days ago on latest obix/devel branch and resend it.