Refactored to remove the global variable

There was a global variable used through out which has now been
refactored out.