refactor ending