Commented and reformatted all code