minor performance optimisations, possible to get further from 3000ms to 2500ms (18% gain) by flattening code (see fastdb). going to stop optimising for now.