refactored code as recommended by michaelklishin for patch submission