changed some ordering around to avoid possible threading output issues