refactor static variables and tidy up code