improvement to replicated code

Just changed some common code into a small function