Bug fix in threading