Some fixes and improvements in viewport handling