rewrite in a functional core/imperative shell manner