Added functional tests and fixed bug discovered