Added documentation and a bit of refactoring