move pipe implementaion of main loop out of main()