Refactoring into monad