updated is_perfect function