Refactoring some repeated code [afurdell]