rewrote for improved readability and to make it pass its tests again