Updated with more synch blocks to prevent race conditions hopefully