Fixes #33 race condition in encode