rewritten generation logic, bugfixes