more refactoring, added some more comments