Reorganized and commented code