refactored a bit for clarity