refactored the previous fix to reduce if statements