js refactor, add data validation