more error checking and updated version