refactored functions & IfElse function