This code is a working version of the inverse functions