See the other comment. Probably using two functions would make the code more readable.