Resolved encoding errors, some refactoring