WORKING VERSION OF new algorithim!