Haskell and Python versions refactored