Moved output into the mutex so it does not get interrupted.