added comments
reorganized code