Initial commit

Works fine in one test case. Futher testing, refactoring and
optimisation to come.