added checks for the upper bounds
and added checks in functions I forgot