Bug fixes + additional refactoring.