modularized code and added some documentation