Moved output related code to where it belongs + Improvements.