finalized the following function