Restructure logic to be more sane