Major refactoring and changes to run_trim.py. Passes all the old test and  runs without crashing but needs some debugging.