Re-applied changes from commit b95c4c3d9c69e1fad096cc7926535cda81625d91