Added code to free malloced arrays for pipes at the end to fix memory leak.