Fixed matter and chain creation.

Also mare some refactorings connected to this.