Added documentation and fixed some bugs