Fixed NaN errors and logic