Fix some subtle Unicode-related errors

Fix for #182, plus a fix for strange errors seen when debugging (see the commit message for details)