refactored javascript, separation of concerns