Add check for unauthorized response. More polish is needed.