fixed some bugs and fixed a variable that wasn't declared at the top