I'm aware of software development principles and best practices. However, in most cases I entered a project that was already on its phase 2, and that already had a significant amount of code written. In addition, in some cases the code and architecture of the project was causing several problems to the application (performance issues, difficulty for maintenance or adding features, lack of coding patterns, in some cases even security issues, etc).
So, because of this we noticed that new features were taking too long to be implemented, due to the complexity of the architecture and difficulty to maintain it.
In most of these projects we use SCRUM as a base for the development methodology; therefore we have constant collaboration with the customers, that help prioritizing and defining many aspects of the tasks.
My question is: since normally the requests for changes or new features come from the customers, is it ok that our team create stories or tasks within the product backlog, regarding refactoring of the code? Or such thing should be done gradually inserted into existing stories?
I'm asking this because in several projects a significant refactoring was really necessary, because the current situation was causing a negative impact on the customer (features taking too long to be developed); also in some cases this need for refactoring was noticed in the middle of the project (no chance of considering this in the project initial phases).