add threading and minor changes