Functional version
Rebuild all logic