added checks to prevent crashes