pattern matching instead of unsafe list functions