fix indentation, add error checks