(cherry picked from commit 1d45ad8f) Some adaptations were made to make the code compile and tests pass.