Restruture the codes to reduce code complexity