Remove unused variables, add better checking