Make this code do proper checks in all cases