fixing some bugs in the code