Refactor generation stuff into separate module