I would suggest to split the following three cases as three functions.