restructured the code and improve some features