added synchronization for outputting