Fixed some bugs and simplified things.