Refactored javascript, tidier