Cleanup code to add and remove script threads