added a type check on casts and made a type tighter