more functional stuff, factoring out loops.