Cleanup: factor common code outside conditional compilation