Moved return code checks to function