Made some changes to make code easier to read and fixed some problems with variables