Reworked newline for readability.  Still trying to keep it at 80-char width.
Added constant value for loop recording temporary files.
Indentation fix.
fixed capitalization.
typo
Moved lock to top of loop to prevent reading empty buffer.