Finished first version of the weighed algo