Change name of test function, code cleanup, add error check