implemented suggested changes + reworked most of the code in order to avoid repeating myself as much as possible.