tidying up the code. removing global variable
more tidying up