Fixed some bugs and introduced a lot of documentation.