Refactoring and cleanup to match stage in tutorial.
Added write locks