added more comments, added error-checking