Fixed crash bugs from 1st round of refactoring