Refactored more, and also added comments for race condition for shared resources