My advice is you should refactor in multiple steps:
- Refactor based on purely functions (argument -> return value), no changing any behaviours here, as well as adding some disciplines to old code base.
- Refactor based on some good practices to enforce guidelines and architectures for future features.