rewrite to more functional