Refactoring compute_output function