Refactor compute_levels