Significant refactoring/cleanup.