Rewrite in functional style