Added output feature, and tweaks to previous code