Added more error checking
Merged Eric