Critical section in encoders + minor fixes