Adding locks in necessary places.