Factored out bitmap compaction into its own method

Also added comments