Added comments, fixed typo, changed appendChild to insertBefore, minor clean up and creation of tests.