Got the final function to work