Refactored and structurized code