Fixed insidious multithread deadlock bug, caused by calling the same locking function inside a locking function which was stupid