Updated solution to prevent values over 1000 being calculated