Fixed some NullPointer / ClassCast  problems