Improve prepare() function