Minor code cleanup and better checking of arguments