Added functions for automatic thread creation and joining