Most of the code here is taken from elsewhere, from snippets or parts of other
projects. I try to give proper credit on the comments of the function, but
please let me know if you find any omissions or mistakes.