Fixed some bugs and added doc-blocks into code