Significant refactor to resolve issues (see TOOD.md)