Improved threading, refactoring