rewrite using class to avoid global varialbes