Revamped algorithm somehwat