Added checks so program fail gracefully instead of crashing