Globalized some variables and removed unecessary function