Substantial refactoring to generatePlan. Added comments.
Started using a linter. Style tweaks and linter directives.