Refactored to match the latest changes in core and fixed minor issues.

refs https://github.com/zikula/core/pull/1407.