Fix all pbs and make code more robust