re-structured code, error handling