Restructure logic and simplify