Refactored to ease embedding in third-party code