Improved performace

Replaced global variable to local variable