finished functional changes for gradient flow. not sure if output correct but structural issues with legacy code have been overcome.