Some refactor to cleanup code