Created by: gaearon
This continues after #861 and should fully fix #765 (closed). Also fixes #698 (closed).