First implementation of generate and main