Clean up intermediate node code

Cleaner implementation (no magic numbers) and better logging.