updated code to fix some bugs