Fixed the problem with functions resetting, cleaned up some code