Fixed compile errors and tidied up new code