corrected some bugs and finished the code