Completed algo (not working yet)