better commented code (with some improvements)