Fixed some latent bugs caused by the refactoring