largely refactored code with old code still in comments