stupid me trying to implement linear time solution, port in last engine