Refactored JS implementation