Improve task1 code based on Rafael's work