fix obvious bugs
Add some comments
Add refactored versioin 2