Refactor viewer code