new version homework 3

Added some new functions to make the code more readable and to avoid
repetitions.