refactoring code and add print function