refactoring code and renaming variables to make saving and restoring easier (actually made it harder in some ways :p)