Added range check to auto target. and tweaked some code.
Tweaked some code