Fixed encoding bugs, minor improvements