Enhanced algorithm. Post added.