Re write in ocaml

Fixes #1