Fixed segfault when changing layouts