optimized encoding
fixes #3