Added more error handling to prevent crashes from incorrect arguments.