Further refactoring and repolishing for clarity.