refactor whole code