refactored into functions; default error return value; freeing memory