Refactored code structure

Moved code around and into functions.
Removed backup_type initial declaration that was unused.