Small fixes
Added lots of error checking