Merge changes I16ce79ab,Ib86a7309

* changes:
  Correctly set the viewport in layers.
  Cleanup, better code reuse.