Combined two previous scripts. Does what should do.