Added type checking and downcasting.