added clearer comments, added checks