Clean up logic in viewer.js