Added more error checking.