Revert "Code removed for now. It introduced another bug."

This reverts commit 492dc2d496f74bd16a16e5f3335923f41c325f2c.