Fix charset issue and code cleanup