Fixed some bugs, typo and junk code. Also added some documentation.