Fixed some bugs and removed unused variables