Major refactoring (incomplete)