This seems to introduce enough duplicate code and nested if statements to refactor into a macro?