Remove unnecessary copying of data and move repetitive code into code generation.

- removed the unnecessary copying of DataFrame arguments.
- tidied code by replacing copy/paste code with code generation for easier future maintenance.