Fixed some bugs. Added some threading