move some functions from C to Haskell