Rewrote in C++.