perltidied and refactored code