Removed global state, used only function local variables and return values