Code clean-up; added function documentations