Significant refactor and simplification.