Some fixes

Bugfix, performance improvement and tidying up of syntax/indentation