verified, porting the code to the functions