add pagination, refactor js code