Refactoring and adding output functions