Added guards and casts