Minor: added more check avoiding ClassCastExceptions