Should work again but with cleaner structure

Added a lot again. Too much to cover but it should be now easier to
implement relations and so on.