added comments, finished output logic