Major refactor of gen, with some todo's