add more error checking and error log