optimized
brushed up partial haskell solution