Rewrote code for better readability and maintainability