slightly re-structuring and adding comments