Added more documentation and minor refactoring