Added more documentation, minor refactoring