added output writing