Rewrite in the most functional way I can