pid/gid mess improved

Not sure if dependencies are correct. please check.
reformatted and fixed