Final, commented version of code