Fixed deadlock and other small bugs