Fixed some errors and tidied code.