Refactored and added documentation