Removed nested if-else and nulls; added pattern matching