     }
     
     func updateKeyCaps(lowercase: Bool) {
-        for (model, key) in self.layout.modelToView {
-            key.text = model.keyCapForCase(!lowercase)
+        if self.layout != nil {
+            for (model, key) in self.layout!.modelToView {
+                key.text = model.keyCapForCase(!lowercase)
+            }
         }
     }
     
     func modeChangeTapped(sender: KeyboardKey) {
-        if let toMode = self.layout.viewToModel[sender]?.toMode {
+        if let toMode = self.layout?.viewToModel[sender]?.toMode {
             self.currentMode = toMode
         }
     }

        }
        else {
            self.forwardingView.frame = orientationSavvyBounds
            self.layout.layoutTemp()
            self.lastLayoutBounds = orientationSavvyBounds
        }
        

            let charactersAreInCorrectState = { () -> Bool in
                let previousContext = (self.textDocumentProxy as? UITextDocumentProxy)?.documentContextBeforeInput
                
                if previousContext == nil || countElements(previousContext!) < 3 {
                    return false
                }
                
