Fixed some bugs after testing
continue working on modifying reference.