first attempt, missing last point and optimize