remove redundant code and free vars