refactored and prepared the code