cleaned up some code and got rid of some global variables. Have to update  next.