Added more checks into code logic, fixed some typos.