Made some more changes to Kansuke's code, but it's getting better.