Add check to prevent TypeError