updated generate function so it can handle any length