Refactor javascript