This ain't pretty but it works...