adding helpful comments and a bit of refactoring