fixed some apoplexical bugs
Add some comments in code