Turns out that the original code is working perfectly for the case when the second material thickness needs to be found while the first material thickness stays constant.