added some documentation and minor rrefactoring