rewriting to use do notation when possible with Result monad