Reduced code redundancy and added documentation