refactored, complete