Try to split refactoring and new features into separate pull requests. It makes it much easier to review. This PR has a mix of refactoring (e.g. moving the `Layout` class into a separate file) and new features (the new `Key` class).