Fix Apply Discard bug and fix all code