Refactor javascript a bit