Completed initial set of refactoring to be able to solve #307 and perhaps #317