Bugfixes for algorithm, added a basic test case for gen().