Modify caption logic, clean up javascript