Contagion versus flight to quality in financial markets