Refactored cleanup module to avoid code duplication and added stacks cleanup code.