Complete version with enumeration algorithm