Refactored code. Created print function