finally a working lazy generation algorithm..needs tests.