fixing some bugs and adding more comments