Ugly generarion logic refactoring