fix encoding problems
refactor
refactored some more