Working version, aside from threading issues