Fixed few bugs. Tidied up program a little.