Fix a race condition in ptask_dispose and two cosmetic changes

Relevant valgrind tests passed, in theory the condition is properly addressed by this patch.