Fixed some typos and improved implementation