Keep in mind that you can have some duplicated computation in this code, as the performance is not critical here.