Refactored check for index number and several things