Added js functions based on Ben's code