Serious memory-usage refactor