Fixed some bugs and redundancy.