Refactored for easier debugging.