Backout most of changes from 4d331ec09b2fc72636f86be628ea2f69381 and 8afe7216. Does not work as expected.