Refactor Angular JS Code