Added double buffering and mutex locking.