layer handling, including rebuilding layers and state/context/layer switching