refactored javascript to new structure