rewrite without using channels