Refactored if-blocks and reduced redundancy