Refactored, significantly more elegant