Simplifed and improved algorithm efficency