added waiting for active API threads on program exit