Various fixes + by-hand pattern test

Not working yet.