Completed code of the function