Fixed some obvious bugs, removed some debugging code.