Refactoring + median filter fixed.