Major refactoring to fix timing issues