moved code from utils.R
tidied up casting a little. insertReturn(). Tests for different things