fixed output routine so it writes things to Z first