refactored code, polished for expansion