refactored and cleared up version