Fix crash of replace and fix concurrency. Closes #49