Made threading work properly, and other various fixes.