added finally clause to prevent deadlock