messy, but working, after a harrowing hour with lots of bad access errors and threading errors