I sometimes lose the thread within my code.
I find that folks with early-onset Alzheimer's generally find programming a challenge...

Assuming you don't fall into that category...

1. Comment what you're doing
2. Comment what your program is doing
3. Comment revisions and alterations
4. Comment section headings

Am I getting thru with this 'comment' business?...

5. Put your subroutines in one place
6. Comment your subroutines as to what they do
7. Give Subroutines a meaningful name

8. Put interrupt handlers in one place
9. Comment Interrupt Handlers as to what they do
10. Give Interrupt Handlers a meaningful name

11. Assign meaningful names to aliases and variables
12. Put your Hardware Defines all in one place
13. Put your Hardware aliases all in one place
14. Put your software defines all in one place
15. Put EEPROM defines all in one place

It's not that difficult to write neat and tidy code that can be logically followed.

People wrote good logical code long before Visual Basic ever came on the scene!

Ever thought of flow-charting your program first as an aid to your writing it? Rarely done because folks are just plain lazy, but it can be an aid if you're having difficulties as can donepezil hydrochloride.