minor optimisations and cleanup
wrong index