rewrite in functional style