Fixed a wrong output with node API and improved code quality