Major refactoring (to be perfected)