restructured and commented some code