rewrite the code using class to aviod global variable