refactored code now fully functional