build the last function with several helping functions