Refactor to print in __str__

Fixes the problem raised in #151.