Improved type detection