Finished first version of the compaction method.