I recommend reading a classic, "Working Effectively With Legacy Code" from Michael Feathers.