Cleaner code, first and third part finished with tests.