more error checking and added cache vars
fixed conflicts