Reorganized code

Also added comments