js code corrected, refactored