Fixed some bugs in new code