Rewriting of S2 with given data structures. Doesn't work yet