Much better implementation