More refactoring; fix object allocation bug