Bug squashing, more internal refactoring