Add more comments

Add comments to document what the code is doing.

Move the locking of the mutex to before getting the map size