Move code into function, fix last unit test