Tentatively solved the optimized method. UNTESTED

Will have to test it soon