Fixed typo
Some cleanup and fixed Heisenbug for real now (hopefully).

Thanks to Denis Shelomovskij for the hint that the implementation was
still not correct.