Refactored javascript one more time