Refactor javascript from feedback