Integrated, cleaned and commented some code