check indexes before accessing array