refactor - break up 80+ line method

generate() method was 80+ lines and very hard to read. To break it up, I had to pass several params in by reference to each sub-method, which isn't ideal either, but still an improvement in terms of readability.

Signed-off-by: Joshua Paling <joshua.paling@gmail.com>