optimized version: 4m21.409s