update printing code and add some tests.