Add JSDocs and refactor a little