Added more error checking