Added tests, and fixed functions where problems were discovered