Java rewrite

It's 40-50% faster, and safer to develop.