added try/finally clause to make sure the lock is always released