added missing synchronized blocks for avoiding concurrency issues