Corrected bugs and created a test function.