optimized the code and add some functions