Rebased to current trunk and applied review comments.