Changed the entire PL resolution algorithm by referencing the sample code, this is the working version