more refactoring before submission. Still works after testing