Refactor js for callbacks