changed lock() to support exceptions
fixed a bug in the new lock() implementation