replace doseq with loop and recur to reduce memory footprint