fixed encodings; factored out code