re-write with utf-8 and speed in mind. see main2