Removed global variables in favor of instance variables. This makes the code more maintainable and making it easier to write unit tests without relying on global state.
Marked the old functions with global state as obsolete, redirecting them to use a new API based on struct in order to rewrite the code using these functions gradually.