cleaned code and added notes to ambiguase parts of code for clarity