Cleaned up/simplified casting code