refactored functions,bug squashing