Moved functions from gui to main