Add more robust type checking