Fixing output and cleaning up code