bug628796: refactored code for simplifying purposes. also should now work on Darwin