Eliminated global vars and minor code fix