I don't think you should add these code into this function. better to have a standalone function for this.