refactor printing and include as a doctest