Refactored and documented