Added some type checking to avoid core dumps.