Fixed thread synchronisation bug