Fixed class cast exception, needed to return on error before doing anything else in the function