Fixed some bugs - removed the dead code.