Added documentation and small refactoring