refactored code partially improved upon style