refine code and improve browser compatibility