Added boundary check

Without the typecasting working