Moved code from JoT repo.