added threading. still a few bugs though.