Moved lock to the top of the event loop to prevent reading from an empty buffer at startup.