restructure some uglier code