more error checking and updated with latest .cs