Reverted while do loop due to logical error