adding pcal and error handeling to the code