Fixed thread handle cleanup, checking for resource cleanup now before resetting flags.