Some tidying up. Still has a memory leak.