Commented and reorganized code