Optmize and improved correctness