Mild DRY-ing, and fixing logic error.