Replaced ugly type checking in routine generation with visitor pattern