Refactored the logic for resizing the viewer.