To make the code more readable, you can return from the function right before the else and bring the rest of the code one level/indent up.

In general, the recommend not putting too big of a block inside an indented if and/or else