refactored and edited for clarity