added documentation and fixed some bugs