Improved javascript version