Fixed some bugs to make unit tests work