better typecasting & error checking