When developing software, after getting the overall customer requirements, I first design a little bit, then add some features, fix bugs, then do refactoring and then repeat. While refactoring, I do not attempt to fix bugs or update the overall design. When I see a serious (i.e. more than a days work) bug or the need to update the design of some part, I write it down to Jira. That way I am not distracted from what I do for short term and do not loose my ideas for the long term. I call this the "zoom-in, zoom-out cycle".