Fix cases where file encoding was not handled properly in the tool

Fixes #563