Refactoring to avoid circular package dependencies; minor bug fixes