added threading, code cleanup