Rewrite to be a bit more functional