Refactored previewer.py to be simpler and more pythonic.

Split off from the previous PR.