refactored for better readability. stricter layering.