Implement given pseudocode