moved lock above Len() check to prevent a race condition with adding new elements