Working implementation, with execution time measurement