Condensed code, removed unused variables