Refactor to avoid unnecessary duplicate ifs