Refactor whole code