compression bug fix and minor refactoring