Ported to 3, working, not yet cleaned up