fixed typos and refactored the code