moved mutex lock below