Tested substitute function. It works. Tweaked a lot of little mistakes