Replaced if with conditional operator

This reduces code duplication.