optimize prepare function