Algorithm gives accurate results