fix code from returning nan