Working version using object-ified JavaScript