Added error checking to fix errors