patched for memory leak, now added a function to free malloc'd memory.
also noticed the missing lock for fork_right, now added.