Added extensive comments regarding each function and tested further.